package com.shoux.api.config;


import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoZhi
 * Date: 2022/3/25
 * Time: 10:35
 * Description: SpringWindSx数据库配置
 */
@Configuration
@MapperScan(basePackages = "com.shoux.api.web.dao.windSx", sqlSessionFactoryRef = "windSxSqlSessionFactory")
public class DataSourceWindSxConfig {

    @Value("${spring.datasource.springwindsx.driver-class-name}")
    String driverClass;
    @Value("${spring.datasource.springwindsx.url}")
    String url;
    @Value("${spring.datasource.springwindsx.username}")
    String userName;
    @Value("${spring.datasource.springwindsx.password}")
    String passWord;

    @Resource
    private HikariWindSxConfig hikariWindSxConfig;

//    @Primary
    @Bean(name = "windSxDataSource")
    @ConfigurationProperties("spring.datasource.springwindsx")
    public DataSource masterDataSource() {

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(passWord);
        dataSource.setMinimumIdle(hikariWindSxConfig.getMinimumIdle());
        dataSource.setMaximumPoolSize(hikariWindSxConfig.getMaximumPoolSize());
        dataSource.setAutoCommit(hikariWindSxConfig.isAutoCommit());
        dataSource.setIdleTimeout(hikariWindSxConfig.getIdleTimeout());
        dataSource.setPoolName(hikariWindSxConfig.getPoolName());
        dataSource.setMaxLifetime(hikariWindSxConfig.getMaxLifetime());
        dataSource.setConnectionTestQuery(hikariWindSxConfig.getConnectionTestQuery());

        return dataSource;
    }



    @Bean(name = "windSxSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("windSxDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();

        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/windSx/*.xml"));
//        sessionFactoryBean.setConfiguration(mybatisProperties.getConfiguration());

        return sessionFactoryBean.getObject();
    }

    @Bean(name = "windSxSqlSessionFactory")
    public SqlSessionTemplate sqlSessionFactoryTemplate(@Qualifier("windSxSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
